package cn.sima.plantmessage.user;



import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import cn.sima.plantmessage.Conf;
import cn.sima.plantmessage.MainActivity;
import cn.sima.plantmessage.R;
import cn.sima.plantmessage.activity.FeedBackActivity;
import cn.sima.plantmessage.cache.ACache;
import cn.sima.plantmessage.dialog.SweetAlertDialog;
import cn.sima.plantmessage.lib.NetWorkUtil;
import net.tsz.afinal.FinalDb;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class UpdateUserinfoActivity extends Activity{
	private EditText edit_text;
	FinalDb db;
	ACache userCache;
	String attr;
	String value;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.fragment_updateuserinfo);
		userCache = ACache.get(this.getApplicationContext(), "userCache");
		String userinfo = userCache.getAsString("user");
		JsonObject userinfoJson =  new JsonParser().parse(userinfo).getAsJsonObject();
		
		Intent it  = getIntent();
		String setTitle = it.getStringExtra("setTitle");
		attr = it.getStringExtra("attr");
		value = userinfoJson.get(attr).getAsString();
		
		ActionBar ab = getActionBar();
		setTitle(setTitle);
		ab.setDisplayHomeAsUpEnabled(true);
		ab.setHomeButtonEnabled(true);


		edit_text = (EditText)findViewById(R.id.edit_text);
		edit_text.setText(value);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		} else {

			// TODO Auto-generated method stub
			value = edit_text.getText().toString().trim();
			if (!value.equals("")) {
				  Log.e("value",value);
				if (NetWorkUtil.networkCanUse(getApplicationContext())) {
					String userinfo = userCache.getAsString("user");
					JsonObject userinfoJson =  new JsonParser().parse(userinfo).getAsJsonObject();
					userinfoJson.remove(attr);
					userinfoJson.addProperty(attr, value);
					String new_userinfo = userinfoJson.toString();
					Log.e("new_userinfo",new_userinfo);
		
					userCache.remove("user");
					userCache.put("user", new_userinfo, 1000);
					
					Ion.with(this)
					.load(Conf.BASE_URL+"/user/updateUser")
					.setBodyParameter("userJson", new_userinfo)
					.asJsonObject()
					.setCallback(new FutureCallback<JsonObject>() {
						   @Override
						    public void onCompleted(Exception e, JsonObject result) {
							   Log.e("result",result.toString());
							   if(result.get("success").getAsBoolean()){
								   
								   String user =   result.get("user").toString();
								   Toast.makeText(getApplicationContext(), "修改成功",
											Toast.LENGTH_SHORT).show();
								   userCache.remove("user");
								   userCache.put("user", user);
								   //放回个人信息
							   }else{
								   Toast.makeText(getApplicationContext(), "修改失败",
											Toast.LENGTH_SHORT).show();
							   }
						    }
						});		

					finish();
					Toast.makeText(getApplicationContext(), "更新成功..", 1)
							.show();
				} else {
				
				}

			} else {
				Toast.makeText(getApplicationContext(), "不能为空....", 1)
				.show();
			}

		}
		return super.onOptionsItemSelected(item);
	}

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.fb, menu);
		return super.onCreateOptionsMenu(menu);
	}
	

}
